<?php
	$dt=time();
	$time_out = 1800;//15 minutes			
	$time_new = $dt - $time_out;			
	
	//clear old history login
	$sql = "update history_login set active = 0 where datetime < $time_new and active = 1";		
	$query=mysql_query($sql);
?>
<script type="text/javascript" language="javascript">
function show()
{
	document.nnf.action="index.php?tc=news_add";
	document.nnf.submit();
}
</script>
<form name="nnf" method="post" action="#">
<table border="0" width="100%" align="center">
<tr>
	<td align="left"><span style="color:#1B78B7; font-size:14px; font-weight:bold">Users</span></td>
	<td align="right">&nbsp;</td>
</tr>
<tr>
	<td background="images/topline.gif" colspan="2" height="11"></td>
</tr>    
</table>
</form>
	<?php
if($_REQUEST['_s'] == 2)
{ ?>
	<table align="center" border="0">
	<tr>
    	<td align="center"><div class="sucmsg"><img src="images/information.png" align="absmiddle" />&nbsp;Deleted Successfully</div></td>
    </tr>    
    </table>
<?php } ?>
<!--
<table width="100%" align="center" border="0" class="list">
<thead>
<tr>
	<td align="center" width="30">#</td>
	<td align="center" width="20"></td>
	<td align="center" width="250">Business Name</td>
    <td align="center" width="200">Email</td>
    <td align="center" width="150">Contact No </td>
    <td align="center" width="150">Username</td>
    <td align="center" width="164">Submitted Date</td>
    <td width="153" align="center">Action</td>
</tr>
</thead>
-->

<?php
include('includes/function.php');
$self="index.php?tc=users&_st=".$_REQUEST['_st'];
$rowsPerPage = 20;
$pageNum = 1;
if(isset($_GET['page']))
{
	$pageNum = $_GET['page'];
}
if($_GET['page']=="")
{
	$pageNum = 1;
}

/*<!--counting the offset-->*/
$offset = ($pageNum - 1) * $rowsPerPage;
$st = $_REQUEST['_st'];
if($st == "" || $st == 2)
{
	$res = mysql_query("select id from users");
}
$query = null;

$direction = "asc";
if ($_GET['sort'])
{
	$sort = $_GET['sort'];	
	if ($_GET['direction'])
	{
		$direction = $_GET['direction'];
	}	
	$query = mysql_query("select u.*, h.active, h.datetime from users u LEFT JOIN history_login h ON u.Email = h.email AND h.active = 1 order by $sort $direction limit $offset , $rowsPerPage");
	
	if (strtolower($direction) == "asc")
	{
		$direction = "desc";
	}
	else
	{
		$direction = "asc";
	}
}
else
{
	$query = mysql_query("select u.*, h.active, h.datetime " .
						"FROM users u LEFT JOIN history_login h ON u.Email = h.email AND h.datetime >= ALL(SELECT datetime FROM history_login n WHERE email = h.email) " .
						"Order by h.active DESC, u.FirstName limit $offset , $rowsPerPage");		
}
?>

<table width="100%" align="center" border="0" class="list">
<thead>
<tr>
	<td align="center" width="30">#</td>
	<td align="center" width="20"><a href="index.php?tc=users&sort=active&direction=<?php echo $direction . ($pageNum > 1 ? "&page=$pageNum" : ""); ?>">Status</a></td>
	<td align="center" width="250"><a href="index.php?tc=users&sort=firstname&direction=<?php echo $direction . ($pageNum > 1 ? "&page=$pageNum" : ""); ?>">Name</a></td>
	<td align="center" width="250"><a href="index.php?tc=users&sort=email&direction=<?php echo $direction . ($pageNum > 1 ? "&page=$pageNum" : ""); ?>">Email</a></td>
    <td align="center" width="200"><a href="index.php?tc=users&sort=university_name&direction=<?php echo $direction . ($pageNum > 1 ? "&page=$pageNum" : ""); ?>">University</a></td>
    <td align="center" width="150"><a href="index.php?tc=users&sort=datetime&direction=<?php echo $direction . ($pageNum > 1 ? "&page=$pageNum" : ""); ?>">Last Login</a></td>
    <td align="center" width="150"><span>Course</span></td>
	<td align="center" width="150"><a href="index.php?tc=users&sort=active_status&direction=<?php echo $direction . ($pageNum > 1 ? "&page=$pageNum" : ""); ?>">Active Status</a></td>
    <td width="153" align="center">Action</td>
</tr>
</thead>

<?php
$i = 1;
while($q = mysql_fetch_array($query))
{ 
	if($q["Status"] == 1) $status = "Approved";
	else $status = "Pending";
	?>
    <tr>
        <td align="center"><?php echo $i+$offset;?></td>
		<td align="center" width="20">
			<?php if ($q["active"]) { ?>
				<img src="../images/dot-online.jpg"/>
			<?php } else { ?>
				<img src="../images/dot-offline.jpg"/>
			<?php } ?>
		</td>
        <td align="left"><span><?php echo trim($q["FirstName"] . " " . $q["LastName"]);?></span></td>
        <td align="left"><a href="mailto:<?php echo $q["Email"];?>"><?php echo $q["Email"];?></a></td>
        <td align="left"><span><?php echo $q["university_name"];?></span></td>
        <td align="center">			
			<span><?php echo $q["datetime"] ? date("h:i:s m/d/Y", $q['datetime']) : "" ;?></span>
		</td>
        <td align="center"><span></span></td>
		<td align="center" width="150"><span><?php echo $q["active_status"] ? "Active" : "Not Active";?></span></td>
        <td align="center"><a href="index.php?tc=user_view&_d=<?php echo $q['Id'];?>&page=<?php echo $pageNum;?>">View</a> | 
        	<a href="index.php?tc=user_view&_d=<?php echo $q['Id'];?>&_a=d&page=<?php echo $pageNum;?>" onclick="return confirm('Are you sure want to delete?')">Delete</a>
        </td>
   	</tr>     
<?php 
	$i++;
}
?>
<?php 
if(mysql_num_rows($res) <= $rowsPerPage){	}
else { ?>
<tr>
    <td colspan="13" align="center"  bgcolor="#E3ECF3">
        <?php echo $paginglink=getPagingLink($res, $rowsPerPage,$pageNum,$self); ?>	
    </td>
</tr>
<?php } ?>
</table> 